<template>
    <el-dialog :title="form.customerId ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label=" 客户姓名" prop="customerName">
        <el-input v-model="form.customerName" placeholder="请输入 客户姓名"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label=" 联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入 联系电话"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label=" 身份证号" prop="identityCard">
        <el-input v-model="form.identityCard" placeholder="请输入 身份证号"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label=" 性别" prop="gender">
        <el-select v-model="form.gender" placeholder="选择性别" style="width:100%">
          <el-option v-for="(item, index) in gender"
                     :key="index"
                     :value="item.value"
                     :label="item.label"/>
        </el-select>
      </el-form-item>
      </el-col>


			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="CarCustomerDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/hu/carCustomer'
import {useDict} from "/@/hooks/dict";
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
		customerId:'',
	  customerName: '',
	  phone: '',
	  identityCard: '',
	  gender: '',
});

// 定义校验规则
const dataRules = ref({
        customerName: [{required: true, message: '不能为空', trigger: 'blur'}],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        identityCard: [
          { pattern: /^(?<![0-9X])(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|7[1-3]|8[1-2]|9[1])\d{4}(?:(?:19|20)\d{2})(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[0-9X](?![0-9X])$/, message: '请输入正确的身份证号', trigger: 'blur' }
        ],

        gender: [{required: true, message: '不能为空', trigger: 'blur'}],
        orderNums: [{required: true, message: '不能为空', trigger: 'blur'}],
})
//定义字典
const { gender } = useDict('gender')
// 打开弹窗
const openDialog = (id: string) => {
  console.log(id)
  visible.value = true
  form.customerId = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取carCustomer信息
  if (id) {
    form.customerId = id
    getcarCustomerData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.customerId ? await putObj(form) : await addObj(form);
		useMessage().success(form.customerId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getcarCustomerData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>